package com.casstime.ec.cloud.discount.config;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.casstime.ec.cloud.config.GenericMybatisConfig;

/**
 * ClassName: MybatisConfig 
 * Function: (mybatis配置类)
 * Reason: ADD REASON(可选).
 *  
 * @author wu.chen 
 * 2018年2月27日 下午1:57:15
 * @version   
 * @since JDK 1.8
 */
@Configuration
@EnableTransactionManagement
public class MybatisConfig extends GenericMybatisConfig {

	//使用时，需要指定name eg:@Transactional("defaultTransactionManager")
	@Bean(name = "defaultTransactionManager")
    public PlatformTransactionManager defaultTransactionManager(DataSource dataSource) {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
		transactionManager.setGlobalRollbackOnParticipationFailure(false);
		return transactionManager;
    }
	
	@Bean
	public SqlSessionFactory getSqlSessionFactoryBean(DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = super.getSqlSessionFactoryBean(dataSource, 
				"com.casstime.ec.cloud.values.amountPromotion", 
				"classpath:com/casstime/ec/cloud/discount/dao/*.xml");
		return sqlSessionFactoryBean.getObject();
	}
	
	@Bean
	public MapperScannerConfigurer getMapperScannerConfigurer() {
		return super.getMapperScannerConfigurer("getSqlSessionFactoryBean", "com.casstime.ec.cloud.discount.dao");
	}
	
}
